package com.zuo.demo01.controller.admin;

import com.alibaba.fastjson.JSON;
import com.zuo.demo01.pojo.Links;
import com.zuo.demo01.service.LinkService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class LinksController {

    @Autowired
    private LinkService linkService;

    @RequestMapping("/table/list/links")
    @ResponseBody
    public Map<String,Object> links_list(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){

        Map<String,Integer> map1 = new HashMap<>();
        map1.put("startIndex",(page-1)*limit);
        map1.put("pageSize",limit);

        List<Links> list = linkService.findAll(map1);

        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",linkService.count());
        map.put("data",list);
        return map;
    }

    @RequestMapping("/links/insert")
    @ResponseBody
    public String links_insert(Links links){
        int result = linkService.insert(links);
        if(result==1){
            return "success";
        }else{
            return "添加失败";
        }
    }

    @RequestMapping("/links/update")
    @ResponseBody
    public String links_update(Links links){
        int result = linkService.update(links);
        if(result==1){
            return "success";
        }else{
            return "修改失败";
        }
    }

    @RequestMapping("/links/delete")
    @ResponseBody
    public String links_delete(@RequestBody List<Links> links){
        Map<String,String> map = new HashMap<>();

        int result =0;
        for (Links link :
                links) {
            result = linkService.delete(link.getId());
            if(result==0){
                map.put("status","error");
                break;
            }
        }
        if(result==1){
            map.put("status","success");
        }


        String remap = JSON.toJSONString(map);
        return remap;
    }




}
